package transaction;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

public class TransactionSynchronizationTest {

    public static void doAfterTransaction(DoAfterTransactionComplete complete) {
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            TransactionSynchronizationManager.registerSynchronization(complete);
        }
    }


    @Transactional
    public void test() {


        TransactionSynchronizationTest.doAfterTransaction(new DoAfterTransactionComplete(() -> {

        }));
    }


}


class DoAfterTransactionComplete implements TransactionSynchronization {

    private Runnable runnable;

    public DoAfterTransactionComplete(Runnable runnable) {
        this.runnable = runnable;
    }

    @Override
    public void afterCompletion(int status) {
        if (status == TransactionSynchronization.STATUS_COMMITTED) {
            this.runnable.run();
        }
    }
}
